<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class InsertData extends Controller
{
    //
    public function __construct(){
    	$this->data = new \App\Model\dataCore();
    	date_default_timezone_set("Asia/Shanghai");
    }

    public function insertData(Request $request){
    	$post = $request->json()->all();

    	$insertParameter = array('classify', 'keys', 'values');
    	foreach ($insertParameter as $key => $value) {
    		if (!isset($post[$value]) && $post[$value] != NULL) {
    			$error['code'] = '041';
    			$error['reason'] = 'Need more key : ' . $value;
    			return $error;
    		}
    	}
    	foreach ($post as $key => $value) {
    		if (!in_array($key, $insertParameter)) {
    			$error['code'] = '042';
    			$error['reason'] = 'There\'s a illegal parameter : '.$key;
    			return $error;
    		}
    	}

    	foreach ($post['values'] as $key => $value) {
    		if (is_array($value)) {
    			$value = json_encode($value);
    		}
    		$where[$key] = $value;
    	}
    	switch ($post['classify']) {
    		case 'order':
    			$this->data->insertData('order', $where);
    			break;
    		case 'doctor':
    			$this->data->insertData('doctor', $where);
    			break;
    		
    		default:
    			# code...
    			break;
    	}
    }
}
